package com.suxin.ratelimit.v2.config;

import com.suxin.ratelimit.v2.EnableRateLimit;
import com.suxin.ratelimit.v2.aop.RateLimitAspect;
import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata;

/**
 * @author Tang
 * @classname RateLimitSelector
 * @description [  ]
 * @date 2022/2/9 16:12
 */
public class RateLimitSelector implements ImportSelector {

    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
        AnnotationAttributes enableRateLimitAttributes = AnnotationAttributes
                .fromMap(importingClassMetadata.getAnnotationAttributes(EnableRateLimit.class.getName()));
        EnableRateLimit.Type type = (EnableRateLimit.Type) enableRateLimitAttributes.get("type");
        switch (type) {
            case DISTRIBUTED:
                return new String[]{RateLimitConfig.DistributedRateLimitConfig.class.getName(), RateLimitAspect.class.getName()};
            case STAND:
            default:
                return new String[]{RateLimitConfig.StandRateLimitConfig.class.getName(), RateLimitAspect.class.getName()};
        }
    }

}